﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using Web.UI;
using Model;
using System.Collections;

namespace Web.admin.BasicManage.Locals
{
    public partial class AddLocal : System.Web.UI.Page
    {
        LocalService localService = new LocalService();
        protected static BoLocal localorg;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string operation = Request["OPERATION"];
                if (operation == null)
                    return;
                string localId = Request["LOC_ID"];
                if (localId == null)
                    return;

                if (operation == "Modify")
                {
                    ModifyLocal(localId);
                }
                else if (operation == "Delete")    
                {
                    DeleteLocal(localId);
                }

            }

        }
      
        protected void ModifyLocal(string localId)
        {
            BoLocal local = localService.getLocal(localId);
            LOCIDBOX.Value = localId;
            localorg = local;
            TextBoxName.Text = local.LOC_NAME;
            TextBoxCode.Text = local.LOC_CODE;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="localId"></param>
        protected void DeleteLocal(string localId)
        {
            BoLocal local = new BoLocal();
            local.LOC_ID = localId;
            localService.DeleteLocal(local);
            Response.Redirect("LocalList.aspx");
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
         protected void ButtonOK_Click(object sender, EventArgs e)
        {
            BoLocal local = new BoLocal();
            local.LOC_NAME = TextBoxName.Text;
            local.LOC_CODE = TextBoxCode.Text;

            BoCheckRepat repeat = new BoCheckRepat();
            repeat.tablename = "Local";
            repeat.constr = "LOC_IsShow";

            Hashtable keyValue = new Hashtable();
            keyValue.Add("LOC_NAME", local.LOC_NAME);
            keyValue.Add("LOC_CODE", local.LOC_CODE);

            repeat.content = keyValue;            
            if (string.IsNullOrEmpty(LOCIDBOX.Value))
            {
                if (checkrepeat.checkrepeatCount(repeat) > 0)
                {
                    Response.Write("<script>alert('此对象已经存在，不允许重复')</script>");
                }
                else
                {
                    localService.CreateLocal(local);
                    Response.Redirect("LocalList.aspx");
                }
            }
            else
            {
                if (localorg.LOC_CODE == local.LOC_CODE && localorg.LOC_NAME == local.LOC_NAME)
                {
                    Response.Write("<script>alert('修改成功')</script>");
                    Response.Redirect("LocalList.aspx");
                }
                else
                {
                    if (checkrepeat.checkrepeatCount(repeat) > 0)
                    {
                        Response.Write("<script>alert('此对象已经存在，不允许重复')</script>");
                    }
                    else
                    {
                        local.LOC_ID = LOCIDBOX.Value;
                        localService.UpdateLocal(local);
                        Response.Redirect("LocalList.aspx");
                    }

                }
            }
           
        }
    }
}